﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Oracle;

public partial class CVReal : ICPRelationshipPage
{
    public CVData m_cvData = new CVData();
   

    #region Properties
    public int m_iCVID
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["cvid"]))
            {
                return Convert.ToInt32(Request.QueryString["cvid"]);
            }
            else
            {
                return -1;
            }
        }
    }
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        m_lsPermittedRoles.Add(ICPUserRole.CAR);
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, Customer Visit - Visit Realization";
        if (!IsPostBack && !IsCallback)
        {
            InitVisitData();
        }

    }
    protected void cbkReal_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = HandleReal(e.Parameter);
    }
	protected void cbkView_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = HandleView(e.Parameter);
    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    protected void InitVisitData()
    {
        Cached.CustomerVisitTable cachedCV = new Cached.CustomerVisitTable();
        DataTable dtVisitData = cachedCV.GetVisitDetailById(m_iCVID);
        if (dtVisitData.Rows.Count > 0)
        {
            m_cvData.iCVID = m_iCVID;
            if (dtVisitData.Rows[0]["VISIT_DATETIME"] != DBNull.Value)
            {
                m_cvData.dtmVisitDatetime = Convert.ToDateTime(dtVisitData.Rows[0]["VISIT_DATETIME"]);
            }
            if (dtVisitData.Rows[0]["VISIT_STATUS"] != DBNull.Value)
            {
                m_cvData.sxVisitStatus = dtVisitData.Rows[0]["VISIT_STATUS"].ToString();
            }
            if (dtVisitData.Rows[0]["CREATE_BY_NAME"] != DBNull.Value)
            {
                m_cvData.sxCreateBy = dtVisitData.Rows[0]["CREATE_BY_NAME"].ToString();
            }
            if (dtVisitData.Rows[0]["RESULT"] != DBNull.Value)
            {
                m_cvData.sxResult = dtVisitData.Rows[0]["RESULT"].ToString();
            }
            if (dtVisitData.Rows[0]["FOLLOW_UP"] != DBNull.Value)
            {
                m_cvData.sxFollowUp = dtVisitData.Rows[0]["FOLLOW_UP"].ToString();
            }
            if (dtVisitData.Rows[0]["P_CUSTOMER_EXPECTATION_ID"] != DBNull.Value)
            {
                m_cvData.iP_Customer_Expectation_ID = Convert.ToInt32(dtVisitData.Rows[0]["P_CUSTOMER_EXPECTATION_ID"]);
            }
            if (dtVisitData.Rows[0]["P_VISIT_PURPOSE_ID"] != DBNull.Value)
            {
                m_cvData.iP_Visit_Purpose_ID = Convert.ToInt32(dtVisitData.Rows[0]["P_VISIT_PURPOSE_ID"]);
            }
            if (dtVisitData.Rows[0]["VISIT_PURPOSE"] != DBNull.Value)
            {
                m_cvData.sxVisitPurpose = dtVisitData.Rows[0]["VISIT_PURPOSE"].ToString();
            }
            if (dtVisitData.Rows[0]["CUSTOMER_EXPECTATION"] != DBNull.Value)
            {
                m_cvData.sxCustomerExpectation = dtVisitData.Rows[0]["CUSTOMER_EXPECTATION"].ToString();
            }
            if (dtVisitData.Rows[0]["COMPANY_NAME"] != DBNull.Value)
            {
                m_cvData.sxCompanyName = dtVisitData.Rows[0]["COMPANY_NAME"].ToString();
            }
            if (dtVisitData.Rows[0]["NAME"] != DBNull.Value)
            {
                m_cvData.sxContactPersonName = dtVisitData.Rows[0]["NAME"].ToString();
            }
            if (dtVisitData.Rows[0]["T_FILE_UPLOAD_ID"] != DBNull.Value)
            {
                m_cvData.iFile_Upload_ID = Convert.ToInt32(dtVisitData.Rows[0]["T_FILE_UPLOAD_ID"]);
            }
            if (dtVisitData.Rows[0]["ORIGINAL_FILENAME"] != DBNull.Value)
            {
                m_cvData.sxOriginalFilename = dtVisitData.Rows[0]["ORIGINAL_FILENAME"].ToString();
            }
            if (dtVisitData.Rows[0]["CONTACT_PERSON_ID"] != DBNull.Value)
            {
                m_cvData.iContactPersonID = Convert.ToInt32(dtVisitData.Rows[0]["CONTACT_PERSON_ID"]);
            }
            if (dtVisitData.Rows[0]["NEW_CONTACT_PERSON_ID"] != DBNull.Value)
            {
                m_cvData.iNewContactPersonID = Convert.ToInt32(dtVisitData.Rows[0]["NEW_CONTACT_PERSON_ID"]);
            }
        }
    }
    private String HandleReal(string sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        InitVisitData();
        foreach (String sxRow in sxSplitted)
        {
            String[] sxRowSplitted = sxRow.Split(new String[] { KGlobal.NEWLINE }, StringSplitOptions.None);

            switch (sxRowSplitted[0].ToUpper())
            {
                case KCV.RESULT:
                    m_cvData.sxResult = sxRowSplitted[1].ToString();
                    break;
                case KCV.FOLLOWUP:
                    m_cvData.sxFollowUp = sxRowSplitted[1].ToString();
                    break;
                case KCV.FILEUPLOAD:
                    m_cvData.iFile_Upload_ID = Convert.ToInt32(sxRowSplitted[1]);
                    break;
                case KCV.EXPECTATION:
                    m_cvData.iP_Customer_Expectation_ID = Convert.ToInt32(sxRowSplitted[1]);
                    break;
            }
        }
        m_cvData.iCVID = m_iCVID;
        DBQuery.CustomerVisitTable.UpdateVisitData(m_cvData);

        //insert into newsfeed
        NewsFeedData nfData = new NewsFeedData();
        nfData.iCustomerId = m_cvData.iCustomerID;
        nfData.iSourceId = (int)NewsFeedSourceType.ICP_CUSTOMER_VISIT;
        nfData.sxContent = String.Format(
            "Contact Person         : {0}<br/>" +
            "Visit Date             : {1}<br/>" +
            "Visit Purpose          : {2}<br/>" +
            "Customer Expectation   : {3}<br/>" +
            "Result                 : {4}<br/>" +
            "Follow Up              : {5}",
            m_cvData.sxContactPersonName,
            m_cvData.dtmVisitDatetime.ToString("dd-MMMM-yyyy, HH:mm:ss"),
            m_cvData.sxVisitPurpose,
            m_cvData.sxCustomerExpectation,
            m_cvData.sxResult,
            m_cvData.sxFollowUp);
        nfData.sxTitle = "Customer Visit has been realized";
        nfData.sxUserId_Poster = m_sxUID;
        nfData.sxUserName_Poster = m_sxUserName;
        DBQuery.NewsFeedTable.SaveNewsFeed(nfData);

        return m_iCVID.ToString();
    }
    private string HandleView(string sxParam)
    {
        String[] sxSplitted = sxParam.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);

        String sxOutput = String.Empty;
        int iCPID = -1;
        bool isNewCP = sxSplitted[2].ToString().ToUpper().Equals("Y");
        switch (sxSplitted[0].ToUpper())
        {
            case "SHOW":
                iCPID = Convert.ToInt32(sxSplitted[1]);
                ContactInfoData cpInfoData = new ContactInfoData();
                Cached.ContactPersonTable cachedCP = new Cached.ContactPersonTable();
                cpInfoData = cachedCP.GetContactInfoData(iCPID, isNewCP);
                sxOutput = CVManager.BuildEditor_CPInfoPopUp(iCPID.ToString(), cpInfoData);
                break;
        }

        return sxOutput;
    }
    #endregion Custom Methods
}
